$(function () {
	var searchParam="";
	var urlParam=location.search;
	if(urlParam){
		searchParam=urlParam;
	}
    $("#jqGrid").Grid({
        url: '../stock/real/list'+searchParam,
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '仓库', name: 'storageName', index: 'storage_id', width: 80},
			{label: '车辆类型', name: 'carType', index: 'car_type', width: 80},
			{label: '销售人员', name: 'personName', index: 'personName', width: 80},
			{label: '出库日期', name: 'outDate', index: 'out_date', width: 80},
			{label: '品牌', name: 'carSerie', index: 'car_serie', width: 80},
			{label: '代码', name: 'code', index: 'code', width: 80},
			{label: '车型', name: 'carModel', index: 'car_model', width: 80},
			{label: '款式', name: 'design', index: 'design', width: 80},
			{label: '车辆vin', name: 'vin', index: 'vin', width: 80},
			{label: '发动机号', name: 'engine', index: 'engine', width: 80},
			{label: '指导价格', name: 'guidePrice', index: 'guide_price', width: 80},
			{label: '状态', name: 'state', index: 'state', width: 80, formatter: function (value) {
					if(value === 1){
						return "闲";
					}else if(value === 2){
						return "定";
					}else if(value === 3){
						return "销售";
					}else{
						return '<span class="label label-danger">错误状态</span>';
					}
				}
			},
			{label: '配置', name: 'config', index: 'config', width: 80},
			{label: '外观', name: 'colour', index: 'colour', width: 80},
			{label: '排量', name: 'displacement', index: 'displacement', width: 80},
			{label: '变速箱', name: 'speedBox', index: 'speedBox', width: 80},
			{label: '动力', name: 'power', index: 'power', width: 80},
			{label: '选装', name: 'optional', index: 'optional', width: 80},
			{label: '备注', name: 'content', index: 'content', width: 80}]
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		stockReal: {},
		ruleValidate: {
//			storageName: [
//				{required: true, message: '名称不能为空', trigger: 'blur'}
//			]
		},
		q: {
		    vin: '',
		    carModel: '',
			carSerie: '',
			design: '',
			config: '',
			colour:'',
			displacement: '',
			speedBox: '',
			power:'',
			personName:''
		}
	},
	methods: {
		saleCar:function(){
			var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}
			confirm('确定要选中的车辆销售出库吗？', function () {
                Ajax.request({
				    url: "../stock/real/sale",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('出库成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.stockReal = {};
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.stockReal.id == null ? "../stock/real/save" : "../stock/real/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.stockReal),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../stock/real/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../stock/real/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.stockReal = r.stockReal;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'vin': vm.q.vin,					
                	'carModel': vm.q.carModel,
					'carSerie': vm.q.carSerie,
					'design': vm.q.design,
					'config': vm.q.config,
					'colour':vm.q.colour,
					'displacement': vm.q.displacement,
					'speedBox': vm.q.speedBox,
					'power':vm.q.power,
					'personName':vm.q.personName},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
        		vin: '',
    		    carModel: '',
    			carSerie: '',
    			design: '',
    			config: '',
    			colour:'',
    			displacement: '',
    			speedBox: '',
    			power:'',
    			personName:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});